package com.example.lenovo.downloaddemo.util;

import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.util.DisplayMetrics;

import com.example.lenovo.downloaddemo.activity.AppStart;
import com.example.lenovo.downloaddemo.activity.MainActivity;
import com.example.lenovo.downloaddemo.http.okhttp.utils.AppContext;

import java.util.Locale;

public class LanguageUtil {
    private Context context;
    Resources resources;
    DisplayMetrics dm;
    Configuration config;
    public LanguageUtil(Context context){
        this.context=context;
    }


    public void changeAppLanguage(String sta) {
        //这是SharedPreferences工具类，用于保存设置，代码很简单，自己实现吧
        // 本地语言设置
        Locale myLocale = new Locale(sta);
        Resources res = AppContext.getInstance().getResources();
        DisplayMetrics dm = res.getDisplayMetrics();
        Configuration conf = res.getConfiguration();
        conf.locale = myLocale;
        res.updateConfiguration(conf, dm);
        Intent intent = new Intent(AppContext.getInstance(), MainActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
        context.startActivity(intent);
        // 杀掉进程
        android.os.Process.killProcess(android.os.Process.myPid());
        System.exit(0);
    }

    /**
     * @param isEnglish true  ：点击英文，把中文设置未选中
     *                  false ：点击中文，把英文设置未选中
     */
    public  void set(String sta) {
        Locale myLocale = new Locale(sta);
        Resources res = AppContext.getInstance().getResources();
        DisplayMetrics dm = res.getDisplayMetrics();
        Configuration conf = res.getConfiguration();
        conf.locale = myLocale;
        res.updateConfiguration(conf, dm);
//        Intent intent = new Intent(AppContext.getInstance(), AppStart.class);
//        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
//        context.startActivity(intent);



    }
}